package com.itheima.hello.hello入门进阶.JavaBean封装.ATM案例;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATMOp {
    private Scanner sc = new Scanner(System.in);
    private ArrayList<ATM> list = new ArrayList<>();
    public void start() {
        while (true) {
            System.out.println("===欢迎使用ATM系统===");
            System.out.println("1.用户登录");
            System.out.println("2.开通账户");
            System.out.println("请选择操作:");
            String command = sc.next();

            switch (command) {
                //1.登录账户
                case "1":
                    login();
                    break;
                case "2":
                    //2.开通账户
                    openAcc();
                    break;
                default:
                    System.out.println("没有该操作");
                    break;
            }
        }
    }
    public void op(ATM atm) {
        while (true) {
            System.out.println("===欢迎进入ATM系统===");
            System.out.println("1.查询账户信息");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.注销卡号");
            System.out.println("7.退出"); // yes/no
            System.out.println("请输入您的操作:");
            String command = sc.next();


            switch (command){
                case "1":
                    //查询账户信息
                    select(atm);
                    break;
                case "2":
                    //存款
                    cun(atm);
                    break;
                case "3":
                    //取款
                    qu(atm);
                    break;
                case "4":
                    //转账
                    transMoney(atm);
                    break;
                case "5":
                    //修改密码
                    upDatapassword(atm);
                    return;
                case "6":
                    //注销卡号
                    if (delet(atm)){
                        return;
                    }
                    break;
                case "7":
                    //退出
                    if (out()){
                        return;
                    }
                    break;
                default:
                    System.out.println("没有该操作");
            }
        }
    }
    //转账
    private void transMoney(ATM atm) {
        while (true) {
            System.out.println("===转账===");
            System.out.println("请输入对方的卡号:");
            String otherCardId = sc.next();
            System.out.println("请输入转账金额:");
            double transmoney = sc.nextDouble();
            if (transmoney>atm.getMoney() || atm.getMoney()<=0){ //判断余额够不够
                System.out.println("您的账户余额不足,当前余额为:" + atm.getMoney());
                break;}
            else {
                ATM atm1 = questBycardId(otherCardId);//通过输入的卡号查询是否存在对应的对象
                if (atm1 == null){  //判断对方卡号对应的对象存不存在
                System.out.println("该卡号不存在,请重新输入");
                continue;}
                else {
                String fn = "";
                fn += atm1.getUsername().charAt(0);
                String name = atm1.getUsername().replace(fn, "*");
                System.out.println("请输入" + name + "的姓氏以确认身份:");  //输入对方姓氏来确认身份
                String firstname = sc.next();
                if (firstname.equals(fn)){ //(atm1.getUsername().charAt(0)：对方的姓氏
                    atm.setMoney(atm.getMoney()-transmoney);
                    atm1.setMoney(atm1.getMoney()+transmoney);
                    System.out.println("转账成功，当前余额为:" + atm1.getMoney());
                    break;}
                else {
                    System.out.println("姓氏输入错误，转账失败,请稍后再试");
                    break;}}
        }}
    }
    //注销卡号
    private boolean delet(ATM atm) {
        while (true) {
            System.out.println("===注销账户===");
            System.out.println("请输入本账户卡号确认身份(输入0返回主菜单):");
            String cardId = sc.next();
            if (cardId.equals("0")){
                return false;
            }
            if (atm.getCardId().equals(cardId)) {  //判断输入的卡号是不是当前的卡号
                    if (atm.getMoney() == 0){  //账户没有钱
                        System.out.println("请确认注销(y/n):");
                        //yes/no
                        String o = sc.next();
                        if(o.equals("Y") || o.equals("y")){
                            //注销成功
                            list.remove(atm);
                            System.out.println("注销成功");
                            return true;}}
                    else {   //账户还有钱
                        System.out.println("您的账户还有余额没有花完，请取完再注销");
                        return false;
                        }}
            else {  //不是当前的卡号
                System.out.println("您输入的卡号不是当前卡号或者卡号不存在,请确认");
                return false;}}
    }
    //修改密码
    private void upDatapassword(ATM atm) {
        while (true) {
            System.out.println("===修改密码===");
            System.out.println("请输入您当前的密码确认身份:");
            String password = sc.next();
            if (atm.getPassword().equals(password)){ //判断输入的密码是不是和当前密码一样
                System.out.println("请您输入新密码:");
                String newpassword = sc.next();
                System.out.println("请您在次确认密码:");
                String oknewpassword = sc.next();
                if (newpassword.equals(oknewpassword)){
                    System.out.println("密码修改成功!请重新登录");
                    atm.setPassword(newpassword);
                    break;
                }
                else {
                    System.out.println("两次密码不一致,请重新修改");
                    continue;
                }
            }
            else { //和当前密码不一样
                System.out.println("密码输入错误,请重新输入");
                continue;
            }
        }
    }
    //取款
    private void qu(ATM atm) {
        while (true) {
            System.out.println("===提现取款===");
            System.out.println("请输入您的取现金额:");
            double newMoney = sc.nextDouble();
            if (atm.getMoney() < newMoney){
                //余额小于输入值
                System.out.println("您的余额不足，当前余额为:" + atm.getMoney());
                break;}
            else {
                //输入值小于最大限额，取款成功
                if (newMoney <= atm.getLimit()){
                    atm.setMoney(atm.getMoney() - newMoney);
                    System.out.println("取现成功, 当前余额为:" + atm.getMoney());
                    return;
                }
                else {
                    //输入值大于最大限额，取款失败
                    System.out.println("取款失败，超出最大限额,请重试");
                    break;
                }
            }
        }
    }
    //存款
    private void cun(ATM atm) {
        System.out.println("===存款===");
        System.out.println("请输入您的存款:");
        double newMoney = sc.nextDouble();
        atm.setMoney(atm.getMoney() + newMoney);
        System.out.println("存款成功,当前您的余额为" + atm.getMoney());
    }
    //查询账户信息
    private void select(ATM atm) {
        System.out.println("===用户信息信息===");
        System.out.println("用户名:" + atm.getUsername());
        System.out.println("性别:" + atm.getSex());
        System.out.println("卡号:" + atm.getCardId());
        System.out.println("余额:" + atm.getMoney());
        System.out.println("限额:" + atm.getLimit());
    }
    //退出
    private boolean out() {
            //判断退出系统
            System.out.println("确认退出(y/n):");
            //yes/no
            String o = sc.next();
            if (o.equals("Y") || o.equals("y")){
                return true;
            }else {
                return false;
            }
        }
    //开户模块
    private void openAcc() {
            ATM atm = new ATM();
            System.out.println("===开通账户===");
            System.out.println("请输入您的用户名（输入0返回上一级）:");
            String username = sc.next();
            if (username.equals("0")){
                return;
            }
            atm.setUsername(username);
            System.out.println("请输入您每次最大取款额度:");
            double limit = sc.nextDouble();
            atm.setLimit(limit);
        while (true) {
            System.out.println("请输入您的性别:");
            char sex = sc.next().charAt(0);
            if (sex=='男' || sex=='女'){
                atm.setSex(sex);
                break;
            }else {
                System.out.println("您输入的性别有误");
            }
        }

        while (true) {
            System.out.println("请输入您的密码:");
            String password = sc.next();
            System.out.println("请再次输入您的密码:");
            String okPassword = sc.next();
            if(password.equals(okPassword)){
                atm.setPassword(password);
                break;}
            else {
                //开户失败
                System.out.println("两次密码不一致，请重新输入");
                break;
            }
        }
        //随机生成八位卡号且不能重复
        String newCardId = createId();//生成不重复的卡号
        atm.setCardId(newCardId);
        //开户成功
        list.add(atm); //把该卡号对应的对象也存入进去
        System.out.println(atm.getUsername() + "恭喜您开户成功,您的卡号是:" + atm.getCardId());
        return;
    }
    //随机生成八位卡号，且不能与集合中的重复
    private String createId() {
        while (true) {
            Random r = new Random();
            String cardId = "";
            for (int i = 0; i < 8; i++) { //随机生成八位卡号
                cardId += r.nextInt(10); //随机0-9
            }
            ATM atm = questBycardId(cardId);
            if (atm == null){
                //说明cardId没有找到对应的用户对象,因此cardId没有重复,可以返回这个caedId作为一个新卡号
                return cardId;
            }
        }
    }
    //登录模块
    private void login() {
                System.out.println("===用户登录===");
                if (list.size() == 0) {
                //没有账户
                System.out.println("系统中还没有一个账户，请先创建账户");
                return;}
        while (true) {
                //有账户
                System.out.println("请输入您的卡号(输入0返回上一级):");
                String cardId = sc.next();
                if (cardId.equals("0")){
                    return;}
                System.out.println("请输入您的密码:");
                String password = sc.next();
                System.out.println("再次输入您的密码:");
                String okPassword = sc.next();
                ATM atm = questBycardId(cardId);
                if (atm == null){
                    System.out.println("该卡号不存在");
                }
                else {
                    if(password.equals(okPassword)){
                    //两次密码一样
                        if (atm.getCardId().equals(cardId) && atm.getPassword().equals(password)) {
                                //登录成功
                                System.out.println(atm.getUsername() + "登录成功,欢迎进入ATM系统，您当前余额为:" + atm.getMoney());
                                op(atm);
                                return;
                            }
                        else {
                            System.out.println("账号或者密码错误,请重新登录");
                            continue;}}
                    else {
                    //两次不一致
                    System.out.println("两次输入的密码不一致，请重新输入");
                    continue;
                }}
 }}
    //判断随机生成的八位卡号是否重复并且返回卡号
    private ATM questBycardId(String cardId) {
        for (int i = 0; i < list.size(); i++) {
            ATM atm = list.get(i);
            if (cardId.equals(atm.getCardId())){
                //存在某个卡号
                return atm;
            }
        } //不存在某个卡号
        return null;//查无此账户
    }
}


